using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DALInvoice
/// </summary>
public class DALInvoice
{
    public DALInvoice()
    {
    }

    public static int InsertInvoice(int deliverableID, string userName, string createdDate, decimal charge)
    {
        SqlCommand command = DAL.CreateCommand();
        command.CommandText = SQL.InsertInvoice;
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@DeliverableID", deliverableID);
        command.Parameters.AddWithValue("@UserName", userName);
        command.Parameters.AddWithValue("@CreatedDate", createdDate);
        command.Parameters.AddWithValue("@Charge", charge);
        return DAL.ExecuteNonQuery(command);
    }

    /// <summary>
    /// Get a dictionary containing weigtIDs and price according to distance and service
    /// </summary>
    /// <param name="distanceID">DistanceID of the distance</param>
    /// <param name="serviceID">ServiceID of the service</param>
    /// <returns>Dictionary object</returns>
    public static Dictionary<int, decimal> GetPriceByServiceAndDistance(int distanceID, int serviceID)
    {
        Dictionary<int, decimal> dic = new Dictionary<int, decimal>();
        SqlCommand cmd = DAL.CreateCommand();
        cmd.CommandText = SQL.GetPriceByServiceAndDistance;
        cmd.Parameters.AddWithValue("@ServiceID", serviceID);
        cmd.Parameters.AddWithValue("@DistanceID", distanceID);
        try
        {
            cmd.Connection.Open();
            SqlDataReader reader =  cmd.ExecuteReader();
            while (reader.Read())
            {
                dic.Add(reader.GetInt32(0), reader.GetDecimal(1));
            }
            return dic;
        }
        catch
        {
            throw;
        }
    }


    /// <summary>
    /// Calculate price for deliverables according to weight, distance and type of service
    /// </summary>
    /// <param name="weight">Weight in grams</param>
    /// <param name="distanceID">DistanceID of the distance</param>
    /// <param name="serviceID">ServiceID of the service</param>
    /// <returns>Price of the deliverables</returns>
    public static decimal CalculatePrice(float weight, int distanceID, int serviceID)
    {
        List<Weight> lstWeights;
        Weight maxWeight;
        Weight exWeight;
        int wID = -1;
        decimal price = 0;
        decimal price01, priceExtent;
        int times = 0;
        Dictionary<int, decimal> dic = GetPriceByServiceAndDistance(distanceID, serviceID);
        lstWeights = DAL.GetDataItems<Weight>(SQL.GetAllWeight, null, null);
        maxWeight  = lstWeights[lstWeights.Count - 1];
        exWeight = lstWeights[0];
        foreach (Weight o in lstWeights)
        {
            if (o.FromWeight != null && o.ToWeight != null)
            {
                if (weight > o.FromWeight && weight <= o.ToWeight)
                {
                    wID = o.WeightID;
                    break;
                }
            }
        }
        if (wID != -1) //The weight is in a fix price table
        {
            price = dic[wID];
        }
        else
        {
            price01 = dic[maxWeight.WeightID];
            priceExtent = dic[exWeight.WeightID];
            times = (int)Math.Ceiling((weight - maxWeight.ToWeight) / (exWeight.WeightExtent));
            price = price01 + priceExtent * times;
        }
        return price;
    }
}
